# for each file, built a database

require "00_common"
require "dbm"
require "fileutils"

# LOCAL_TIME_STAMP 	= 1
# SESSION_ID 			= 2
# SESSION_START_TIME 	= 3
# CLIENT_IP			= 4
# SERVER_IP			= 5
# L4_PROTO			= 6
# CLIENT_PORT			= 7
# SERVER_PORT			= 8
# CLIENT_PAYLOAD_HEX 	= 9
# SERVER_PAYLOAD_HEX 	= 10
# PROTOCOL			= 11
# CLIENT_PAYLOAD 		= 12
# SERVER_PAYLOAD 		= 13


DATA_DIR = "./Jan_6/"
DB_DIR = "./db_files/"


module BuildUserDB
  def BuildUserDB.clear_user_db
    # move the exist dbs. 
    begin
      File.delete "db_user_*"
    rescue Exception => e
      puts "no raw db to delete"
    end
  end

  def BuildUserDB.build_file(file_name)
    if Common.db_exist?(file_name)
      # do nothing
      puts "db exist: " + file_name
    else
      data_file = File.open(DATA_DIR + file_name)

      db_file_name = "db_user_" + file_name
      db_file = DBM.open(db_file_name)

      puts data_file.path
      counter = 0

      begin
        while (line = data_file.readline)
          # puts "to read line:"
          if line.length > 0 and line[0,1] != '#'
            # puts "in if: " + counter.to_s

            if line[-1,1] == "|"
              line = line + "NULL"
            end

            fields = line.split('|')
            # puts "split"

            # user_id = fields[CLIENT_IP] + "|" + fields[CLIENT_PORT]
            user_id = fields[SESSION_ID]

            # puts "db"
            old_value = db_file[user_id]
            if old_value
              db_file[user_id] = old_value + "$|$" + line 
            else
              db_file[user_id] = line 
            end 

            counter = counter + 1

            if counter % 100000 == 0
              puts counter
            end
          end # of line doesn't start with #
        end # of each line
      rescue Exception => re
        puts "===> end of file"
        data_file.close
      end # of begin

      # close and move db
      # puts "database size : " + db_file.size.to_s
      puts "===> to close the data file...."

      db_file.close
      # FileUtils.mv db_file_name + ".db" , "./db_files/" + db_file_name + ".db"

      puts "===> to move the data file...."
      BuildUserDB.move_database db_file_name + ".db", "./db_files/"
    end # of if else
  end # of build_file

  def BuildUserDB.move_database(file_name, dest)
    FileUtils.mv file_name, dest + file_name
  end
end # of module


begin
  file_list = DBM.open("db_file_list")
  
  BuildUserDB.clear_user_db
  

  #
  # loop for each file
  #
  (0..file_list.size - 1).each do |key|
    file_name = file_list[key.to_s]
    BuildUserDB.build_file(file_name)
  end # of each file

  file_list.close
rescue Exception => e
  puts e.to_s
end